<?php
include("../db_connect.php");
include("../class/objName.php");
?>
<script>
var brand = $("#_brand");
var parts = $("#_parts");
var model =	$("#_model");
var brand2 = $("#checkbrand");

$(document).ready(function () {

/**/
$("#brand_add").click(function() {
	
	if(brand.val() == "" || brand.val() == " " ){
		alert("please input a proper brand");
		brand.focus();
	}else{
		itemexisit("tblbrand",brand.val(),"");
	}

});

/**/
$("#model_add").click(function() {
	
	if(brand2.val() == "null" || brand2.val() == ""  ){
		alert("please select a brand");
	}
	else if(model.val() == "" || model.val() == " " ){
		alert("please input a proper model");
		model.focus();
	}
 
	else{
	
		itemexisit("tblmodel",model.val(),brand2.val());
	}

});

/**/
$("#parts_add").click(function() {
	if(parts.val() == "" || parts.val() == " " ){
		alert("please input a proper parts");
		parts.focus();
	}else{
		itemexisit("tblparts",parts.val(),"");
	}
});

$(".brandedit").click(function (){
	
	var e = $(this).attr("name");
	var data = e.split("+");
	var bn = data[0];
	var bi = data[1];
	var tbl = "tblbrand";
	bn = parsewhitespace(bn);
	
		
	$("#msgbox").load("admin/editbox.php?bi="+bi+"&tbl="+tbl+"&bn="+bn);
	$("#msgbox").dialog({
		modal:true,
		title:"",
		width:350,
		height: 200,
		show: "fade",
		hide: "fade",
		buttons: {
		
		},
	});
	

	
});

$(".partsedit").click(function (){
	
	var e = $(this).attr("name");
	var data = e.split("+");
	var bn = data[0];
	var bi = data[1];
	var tbl = "tblparts";
	bn = parsewhitespace(bn);
	
		
	$("#msgbox").load("admin/editbox.php?bi="+bi+"&tbl="+tbl+"&bn="+bn);
	$("#msgbox").dialog({
		modal:true,
		title:"",
		width:350,
		height: 200,
		show: "fade",
		hide: "fade",
		buttons: {
		
		},
	});
	

	
});

$(".modeledit").click(function (){
	
	var e = $(this).attr("name");
	var data = e.split("+");
	var bn = data[0];
	var bi = data[1];
	var tbl = "tblmodel";
	bn = parsewhitespace(bn);
	
		
	$("#msgbox").load("admin/editbox.php?bi="+bi+"&tbl="+tbl+"&bn="+bn);
	$("#msgbox").dialog({
		modal:true,
		title:"",
		width:350,
		height: 200,
		show: "fade",
		hide: "fade",
		buttons: {
		
		},
	});
	

	
});

});/*end of document */

function parsewhitespace(bn){
	var a = new Array();
	a = bn.split(" ");
	return a;
}

function itemexisit(tbl,item,brandid){
	
	if(tbl == "tblbrand"){
		$.ajax({
			url:"admin/existbrandmodelparts.php?tbl="+tbl+"&item="+item,
			success:function(data){
				if(data=="merun"){
					
					alert("Brand already exsisting");
					brand.focus();
				}else{
			
					additem(tbl,item,brandid);
				}
			}
		});
	}
	else if(tbl == "tblmodel"){
		$.ajax({
			url:"admin/existbrandmodelparts.php?tbl="+tbl+"&item="+item+"&brandid="+brandid,
			success:function(modeldata){
				if(modeldata == "merun"){
					alert("Model already exsisting");
					model.focus()
				}else{
					additem(tbl,item,brandid);
				}
			}
		});
	}
	else if(tbl == "tblparts"){
		$.ajax({
			url:"admin/existbrandmodelparts.php?tbl="+tbl+"&item="+item,
			success:function(data){
				if(data == "merun"){
					alert("Parts already exsisting");
					parts.focus();
				}else{
					additem(tbl,item,brandid);
				}
			}
		});
	}

}
function additem(tbl,item,brandid){
	if(tbl == "tblbrand"){
		$.ajax({
			url:"admin/addbrandmodelparts.php?tbl="+tbl+"&item="+item,
			success:function(data){
				if(data == "exist"){
					alert("Something Went wrong");
				}else{
					alert("Success fully added");
					location.reload();
					
				}
			}
		});
	}
	else if(tbl == "tblmodel"){
		$.ajax({
			url:"admin/addbrandmodelparts.php?tbl="+tbl+"&item="+item+"&brandid="+brandid,
			success:function(data){
				if(data == "exist"){
						alert("Something Went wrong");
				}else{
					alert("Success fully added");
					location.reload();
				}
			}
		});
	}
	else if(tbl == "tblparts"){
		$.ajax({
			url:"admin/addbrandmodelparts.php?tbl="+tbl+"&item="+item,
			success:function(data){
				if(data == "exist"){
						alert("Something Went wrong");
				}else{
					alert("Success fully added");
					location.reload();
				}
			}
		});
	}
}


</script>
<style>
#_brand,#_model,#_parts{
width:190px;
margin-left:1px;
}
#brandmodelparts{
background-color:white;
border:red;
}
#checkbrand {
    border:1px solid #ccc;
    vertical-align:top;
    height:70px;
	width:430px;
	overflow-y: scroll;
}
.view{
width:210px;
height:400px;
overflow-y: auto;
list-style:none;
list-style-type:none;
background-color:;
margin-top:10px;
color:#00F;
border:red;
}
.dview {
width:430px;
height:350px;
overflow:auto;
list-style:none;
list-style-type:none;
background-color:
margin-top:10px;
color:#00F;
border:red;
}
#odd{
background-color:#f3d2d2;
}
#even{
background-color:#d6d9da;
}
</style>
<div id="msgbox"></div>
<table id="brandmodelparts" width="100%" height="400px" border="1" >
	<tr>
		<td valign="top">
			<label><h5>Add Brand</h5></label>
			<input type="text" id="_brand"/>
			<button id="brand_add">Add brand</button>
			<div>
			</div>
			<div id="" class="view " >
			<ul  style="list-style: none outside none; margin-left: -40px;">
				<?php
				$sql = mysql_query("select * from tblbrand order by brand");
				$true = true;	
					while($row = mysql_fetch_array($sql)){
					
					if($true){
					echo '<li id="even">'.$row['brand'].
						 '<a href="#'.$row['brand'].'+'.$row['id'].'" style="float:right; color:red; " class="brandedit" name="'.$row['brand'].'+'.$row['id'].'">Edit</a></li>';
					$true = false;
					}else{
						echo '<li id="odd" >'.$row['brand'].
							 '<a href="#'.$row['brand'].'+'.$row['id'].'" style="float:right; color:red;" class="brandedit" name="'.$row['brand'].'+'.$row['id'].'">Edit</a></li>';
						$true = true;
					}
					
					
					}
				?>
			</ul>
			</div>
		</td>
		<!--Adding Model-->
		<td valign="top">
			<label id=""><h5>Add Model</h5></label>
			
			<select id="checkbrand"  multiple="true" value="checkbrand" >
			<div>
				<?php 
					$sql = mysql_query("SELECT * FROM tblbrand order by brand");
						$opt = '<option id="optbrand" name="none" value="">Select</option> ';
						while($row = mysql_fetch_array($sql) ){
						$opt .= '<option value="'.$row["id"].'" >'.$row["brand"].'</option>';
						}
					echo $opt;
				?>
			</div>
			</select>
			<input type="text" id="_model"/>
			<button id="model_add">Add Model</button>
			<div id="" class="dview">
				<ul  style="list-style: none outside none; margin-left: -40px;">

					<?php
					$sql = mysql_query("select * from tblmodel order by  brandId,modelname");
					$true = true;	
						while($row = mysql_fetch_array($sql)){
						$obj = new objName();
						$b = $obj->get_product_brand($row['brandId']);		
						if($true){
						echo '<li id="even">'.$b. '-'.$row['modelname'].
							 '<a href="#" style="float:right; color:red; " class="modeledit" name="'.$row['modelname'].'+'.$row['id'].'">Edit</a></li>';
						$true = false;
						}else{
							echo '<li id="odd" >'.$b. '-'.$row['modelname'].
								 '<a href="#" style="float:right; color:red;" class="modeledit" name="'.$row['modelname'].'+'.$row['id'].'">Edit</a></li>';
							$true = true;
						}
						
						
						}
					?>
				</ul>
			</div>
		</td>
		<!--Adding parts-->
		<td valign="top">
			<label><h5>Add Parts</h5></label>
			<input type="text" id="_parts"/>
			<button id="parts_add">Add Parts</button>
			<div id="" class="view " >
			<ul  style="list-style: none outside none; margin-left: -40px;">
				<?php
				$sql = mysql_query("select * from tblparts order by parts");
				$true = true;	
					while($row = mysql_fetch_array($sql)){
					
					if($true){
					echo '<li id="even">'.$row['parts'].
						 '<a href="#" style="float:right; color:red; " class="partsedit" name="'.$row['parts'].'+'.$row['id'].'">Edit</a></li>';
					$true = false;
					}else{
						echo '<li id="odd" >'.$row['parts'].
							 '<a href="#" style="float:right; color:red;" class="partsedit" name="'.$row['parts'].'+'.$row['id'].'">Edit</a></li>';
						$true = true;
					}
					
					
					}
				?>
			</ul>
			</div>
		</td>
	</tr>
</table>

